Imports System
Imports System.Text
Imports System.Data
Imports System.Collections.Generic

Imports Framework

#Region "Enum: Primary Key Values"
'<CLSCompliant(True)> _
'Public Enum EUser
'    Huey = 1
'    Duey = 2
'    Louie = 3
'End Enum
#End Region

'Table-Row Class (Customisable half)
Partial Public Class CUser

#Region "Constants"
#End Region

#Region "Constructors (Public)"
    'Default Connection String
    Public Sub New()
        MyBase.New()
    End Sub
    
    'Alternative Connection String
    Public Sub New(ByVal dataSrc as CDataSrc)
        MyBase.New(dataSrc)
    End Sub
    
    'Hidden (UI code should use cache instead)
    Protected Friend Sub New([userId] As Integer)
        MyBase.New([userId])
    End Sub
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, [userId] As Integer)
        MyBase.New(dataSrc, [userId])
    End Sub
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, ByVal [userId] As Integer, ByVal txOrNull As IDbTransaction)
        MyBase.New(dataSrc, [userId], txOrNull)
    End Sub
#End Region

#Region "Default Values"
    Protected Overrides Sub InitValues_Custom()
        'm_sampleDateCreated = DateTime.Now
        'm_sampleSortOrder   = 0
    End Sub
#End Region

#Region "Default Connection String"
    Protected Overrides Function DefaultDataSrc() As CDataSrc
        Return CDataSrc.Default
    End Function
#End Region

#Region "Properties - Relationships"    
    'Relationships - Foriegn Keys (e.g parent)


    'Relationships - Collections (e.g. children)
    Public ReadOnly Property [Comments] As CCommentList
        Get
            Return CComment.Cache.GetByUserId(Me.UserId)
        End Get
    End Property
    Protected Function Comments_(ByVal tx As IDbTransaction) As CCommentList   'Only used for cascade deletes
        With New CComment(DataSrc)
            Return .SelectByUserId(Me.UserId, tx)
        End With
    End Function

    Public ReadOnly Property [Historys] As CHistoryList
        Get
            Return CHistory.Cache.GetByUserId(Me.UserId)
        End Get
    End Property
    Protected Function Historys_(ByVal tx As IDbTransaction) As CHistoryList   'Only used for cascade deletes
        With New CHistory(DataSrc)
            Return .SelectByUserId(Me.UserId, tx)
        End With
    End Function
#End Region

#Region "Properties - Customisation"
    'Derived/ReadOnly (e.g. xml classes, presentation logic)
    Public ReadOnly Property NameAndCount As String
        Get
            Return CUtilities.NameAndCount(UserName, CItem.Cache.GetByAssignedToUserId123(UserId))
        End Get
    End Property
#End Region

#Region "Save/Delete Overrides"
    Public Overrides Sub Delete(ByVal txOrNull As IDbTransaction)
        'Use a transaction if none supplied
        If txOrNull Is Nothing Then
            BulkDelete(Me)
            Exit Sub
        End If

        'Cascade-Delete (all child collections)
        Me.Comments_(txOrNull).DeleteAll(txOrNull)
        Me.Historys_(txOrNull).DeleteAll(txOrNull)

        'Normal Delete
        MyBase.Delete(txOrNull)
    End Sub
#End Region

#Region "Custom Database Queries"
    '(Not normally required for cached classes, use list class for searching etc)
    'For Stored Procs can use: MakeListTyped (matching schema), or DataSrc.ExecuteDataset (reports etc)
    'For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
#End Region

#Region "Searching (Optional)"
    'For cached classes, custom seach logic resides in static methods on the list class
    ' e.g. CUser.Cache.Search("..")

    'See also the auto-generated methods based on indexes
    ' e.g. CUser.Cache.GetBy...
#End Region

#Region "Caching Details"
    'Cache Key
    Friend Shared CACHE_KEY As String = GetType(CUser).ToString 'TABLE_NAME

    'Cache data
    Private Shared Function LoadCache() As CUserList
        Return New CUser().SelectAll()
    End Function
    
    'Cache Timeout
    Private Shared Sub SetCache(ByVal value As CUserList)
        If Not IsNothing(value) Then value.Sort()
        CCache.Set(CACHE_KEY, value) 'Optional parameter can override timeout (otherwise uses config-settings, which default to 3hrs)
    End Sub
    
    'Helper Method
    Private Function CacheGetById(ByVal list As CUserList) As CUser
        Return list.GetById(Me.UserId)
    End Function
#End Region

#Region "Cloning"
    Public Function Clone(ByVal target as CDataSrc, ByVal txOrNull as IDbTransaction) As CUser ', parentId As Integer) As CUser
        'Shallow copy: Copies the immediate record, excluding autogenerated Pks
        Dim copy As New CUser(Me, target)

        'Deep Copy - Child Entities: Cloned children must reference their cloned parent
        'copy.SampleParentId = parentId

        copy.Save(txOrNull)

        'Deep Copy - Parent Entities: Cloned parents also clone their child collections
        'Me.Children.Clone(target, txOrNull, copy.UserId)

        Return copy
    End Function
#End Region

#Region "ToXml"
    Protected Overrides Sub ToXml_Custom(ByVal w As System.Xml.XmlWriter)
        'Store(w, "Example", Me.Example)
    End Sub
#End Region

End Class
